#include "ElementFactory.h"
#include "Miscellaneous.h"
#include "ActiveX.h"
#include "ImageElement.h"
#include "InputCheckBox.h"
#include "InputHip.h"
#include "InputRadio.h"
#include "InputText.h"
#include "SelectElement.h"

using namespace std;

CElementFactory::CElementFactory(void)
{
}


CElementFactory::~CElementFactory(void)
{
}

unique_ptr<CElement> CElementFactory::Create(IXmlReader* pReader) throw(...)
{
	unique_ptr<CElement> e;
	//
	wstring name = CMiscellaneous::GetLocalName(pReader);
	if(name == L"object")
	{
		e.reset(new CActiveX());
	}
	else if(name == L"select")
	{
		e.reset(new CSelectElement());
	}
	else if(name == L"input")
	{
		wstring type = CMiscellaneous::GetAttributeValue(pReader, L"type");
	
		if(type == L"")
			type = L"text";
		
		if(type == L"text")
			e.reset(new CInputText());
		else if(type == L"checkbox")
			e.reset(new CInputCheckBox());
		else if(type == L"hip")
			e.reset(new CInputHip());
		else if(type == L"radio")
			e.reset(new CInputRadio());
		else
			throw wstring(L"CElementFactory::Create() -> Unknown type of input ->") + type;
	}
	else if(name == L"img")
	{
		e.reset(new CImageElement());
	}
	else
	{
		throw wstring(L"CElementFactory::Create() -> Unknown local name ->") + name;
	}
	//
	// Real initialize element
	// NOTE: we cannot initialize it during the constructor of the class because
	//	there is pure virtual method defined in the class
	if(e)
		e->InitElementFromXml(pReader);
	//
	return e;
}